﻿using System;
using CodingDojo.Domain.KataAlarmClock;
using CodingDojo.Domain.KataFizzBuzz;
using CodingDojo.Domain.KataRomanNumerals;

namespace CodingDojo.Client
{
  public class Program
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("press [1] if you would like to run KataFizzBuzz ...");
      Console.WriteLine("press [2] if you would like to run KataRomanNumerals ...");
      Console.WriteLine("press [3] if you would like to run KataAlarmClock ...");
      var key = Console.ReadKey();
      Console.WriteLine();
      if (key.KeyChar == '1')
      {
        RunKataFizzBuzz();
      }
      if (key.KeyChar == '2')
      {
        RunKataRomanNumerals();
      }
      if (key.KeyChar == '3')
      {
        RunKataAlarmClock();
      }
      else
      {
        Console.WriteLine("bye bye ...");
      }
      PressAnyKeyToContinue();
    }

    private static void RunKataAlarmClock()
    {
      Console.WriteLine("*** KataAlarmClock ***");
      var now = DateTime.Now;
      const int id = 101;

      var repository = new AlarmRepository();
      repository.Add(new Alarm(id, new TimeSpan(now.Hour, now.Minute, now.Second + 15), "pippo pluto paperino"));
      
      IAlarmsManager manager = new AlarmsManager(repository);
      manager.TurnOn(id);
      while (true)
      {
        now = DateTime.Now;
        var alarm = manager.CheckAlarms(new TimeSpan(now.Hour, now.Minute, now.Second));
        if (alarm != null)
        {
          Console.WriteLine(alarm.Label);
          Console.WriteLine("press [1] if you would like to snooze ...");
          Console.WriteLine("press [2] if you would like to stop ...");
          var key = Console.ReadKey();
          Console.WriteLine();
          if (key.KeyChar == '1')
          {
            manager.Snooze(alarm.Id);
          }
          if (key.KeyChar == '2')
          {
            manager.TurnOff(alarm.Id);
          }
          if (manager.AllAlarmsOff)
            break;
        }
        System.Threading.Thread.Sleep(1000);
      }
      Console.WriteLine("*** /KataAlarmClock ***");
    }

    private static void RunKataRomanNumerals()
    {
      Console.WriteLine("*** KataRomanNumerals ***");
      while (true)
      {
        Console.Write("integer: ");
        var integer = Console.ReadLine();
        if (integer == "x")
          break;
        try
        {
          var romanNumerals = new RomanNumerals();
          var roman = romanNumerals.IntegerToRoman(Convert.ToInt32(integer));
          Console.WriteLine(string.Format("roman: {0}", roman)); // todo add validation
        }
        catch (Exception)
        {
          Console.WriteLine("invalid input ...");
        }
      }
      Console.WriteLine("*** /KataRomanNumerals ***");
    }

    private static void RunKataFizzBuzz()
    {
      var fizzBuzz = new FizzBuzz();
      for (var i = 1; i < 100; i++)
        Console.WriteLine(fizzBuzz.GetNumber(i));
    }

    private static void PressAnyKeyToContinue()
    {
      Console.WriteLine("press any key to continue ...");
      Console.ReadKey();
    }
  }
}
